// vue.config.js
module.exports = {
	// console.log(process.env.IS_MOBILE,'process.env.IS_MOBILE');
	configureWebpack: (config) => {
	
		if (process.env.NODE_ENV === "production") {
			changeHtmlWebpackPluginChunks(config);
		}
	},
	chainWebpack: (config) => {
		if (process.env.NODE_ENV === "production") {
			config.optimization.splitChunks({
				chunks: "all", // 必须三选一： "initial"(同步包) | "all"(推荐，同步或异步包) | "async" (默认就是async，异步包)
				automaticNameDelimiter: "~", // 打包分隔符
				name: function(module, chunks, cacheGroupKey) {
					const moduleFileName = module
						.identifier()
						.split("/")
						.reduceRight((item) => item);
					const allChunksNames = chunks.map((item) => item.name).join("~");
					return `${moduleFileName}`;
				},
				cacheGroups: {
					vendors: {
						name: "chunk-vendors",
						chunks: "initial",
						reuseExistingChunk: true,
						enforce: true, // 遇到重复包直接引用，不重新打包
						priority: 0, // 打包优先级权重值
						minChunks: 1,  // 引用超过一次直接打包到chunk中
						minSize: 20000,
						// maxSize: 20000
					},
					pages: {
						name: "chunk-pages",
						test: /pages\.json$/,
						chunks: "all",
						reuseExistingChunk: true,
						enforce: true,
						priority: 90,
						minChunks: 1,
						minSize: 10000,
					},
					"node-rsa": {
						name: "chunk-node-rsa",
						test: /node-rsa/,
						chunks: "all",
						reuseExistingChunk: true,
						enforce: true,
						priority: 70,
					},
					buffer: {
						name: "chunk-buffer",
						test: /buffer|is-buffer/,
						chunks: "all",
						reuseExistingChunk: true,
						priority: 90,
						minChunks: 1,
						minSize: 15000,
						enforce: true,
					},
					"core-js": {
						name: "chunk-core-js",
						test: /core-js/,
						chunks: "initial",
						reuseExistingChunk: true,
						enforce: true,
						priority: 70,
					},
					"uni-h5": {
						name: "chunk-uni-h5",
						test: /uni-h5/,
						chunks: "initial",
						reuseExistingChunk: true,
						enforce: true,
						priority: 80,
					},
					vue: {
						name: "chunk-vue",
						test: /vue-cli-plugin-uni/,
						chunks: "initial",
						reuseExistingChunk: true,
						enforce: true,
						priority: 85,
					},
					bn: {
						name: "chunk-bn",
						test: /bn/,
						chunks: "initial",
						reuseExistingChunk: true,
						enforce: true,
						priority: 70,
					},
				},
			});
			
			// 提取公共的runtime
			config.optimization.runtimeChunk("single");
		}
		
	},
};


function changeHtmlWebpackPluginChunks(config) {
	const plugins = config.plugins;
	const chunkArr = [
		"runtime",
		"chunk-vue",
		"chunk-uni-h5",
		"chunk-core-js",
		"chunk-node-rsa",
		"chunk-buffer",
		"chunk-bn",
		"chunk-pages",
	];

	plugins.forEach((item, index) => {
		// 为HtmlWebpackPlugin
		if (
			item.options &&
			Object.prototype.toString.call(item.options) === "[object Object]" &&
			Object.hasOwnProperty.call(item.options, "filename") &&
			Object.hasOwnProperty.call(item.options, "chunks") &&
			item.options.filename === "index.html"
		) {
			const oldChunks = item.options.chunks || [];
			// 修改chunks引入
			item.options.chunks = [...chunkArr, ...oldChunks];
		}
	});
}